﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace NEWS.DAL
{
    public class News
    {
        public static bool add(NEWS.Model.News news)
        {
            bool result = false;

            string strsql = "insert into t_news (title,menu,more,state,adddate) values ('"+news.Title+"'," + news.Menu + ",'" + news.More + "',"+news.State+",'"+news.Adddate+"')";

            int i = NEWS.MsSqlHelper.YFMsSqlHelper.ExecuteSql(strsql);

            if (i > 0)
            {
                result = true;
            }

            return result;
        }

        public static bool del(int id)
        {
            bool result = false;

            string strsql = "delete from t_news where id = " + id + "";

            int i = NEWS.MsSqlHelper.YFMsSqlHelper.ExecuteSql(strsql);

            if (i > 0)
            {
                result = true;
            }

            return result;

        }

        public static bool update(NEWS.Model.News news)
        {
            bool result = false;

            string strsql = "update t_news set title='"+news.Title+"',menu="+news.Menu+",more='"+news.More+"',state='"+news.State+"' where id="+news.Id+"";

            int i = NEWS.MsSqlHelper.YFMsSqlHelper.ExecuteSql(strsql);

            if (i > 0)
            {
                result = true;
            }

            return result;
        }

        public static List<NEWS.Model.News> list()
        {
            string strsql = "select * from t_news";
            DataTable dataTable = NEWS.MsSqlHelper.YFMsSqlHelper.Query(strsql).Tables[0];
            return Dttolist(dataTable);
        }

        public static List<NEWS.Model.News> list(int menu)
        {
            string strsql = "select * from t_news where menu ="+menu+"";
            DataTable dataTable = NEWS.MsSqlHelper.YFMsSqlHelper.Query(strsql).Tables[0];
            return Dttolist(dataTable);
        }

        public static NEWS.Model.News GetNews(int id)
        {
            string strsql = "select * from t_news where id = '" + id + "'";
            DataTable dataTable = NEWS.MsSqlHelper.YFMsSqlHelper.Query(strsql).Tables[0];
            return Dttolist(dataTable)[0];
        }

        public static List<NEWS.Model.News> Dttolist(DataTable dt)
        {
            List<NEWS.Model.News> list = new List<Model.News>();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                NEWS.Model.News news = new Model.News();

                news.Id = int.Parse(dt.Rows[i]["id"].ToString());
                news.State = int.Parse(dt.Rows[i]["state"].ToString());
                news.Title = dt.Rows[i]["title"].ToString();
                news.Adddate = DateTime.Parse(dt.Rows[i]["adddate"].ToString());
                news.More = dt.Rows[i]["more"].ToString();

                int menu ;
                menu = int.Parse(dt.Rows[i]["menu"].ToString());
                news.Menu = menu;

                list.Add(news);
            }

            return list;
        }
    }
}
